package com.yklab.library.storage.preference;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.yklab.library.base.BaseApplication;
import com.yklab.library.app.domain.CommonUser;


/**
 * Created by 59153 on 2017/12/27.
 */

public class PreferenceHelper {

    private final static String TAG = "SharePreferencesUtils";

    private static PreferenceHelper instance;
    private CommonUser mUser = null;

    private PreferenceHelper() {
    }

    public synchronized static PreferenceHelper getInstance() {
        if (instance == null) {
            instance = new PreferenceHelper();
        }
        return instance;
    }

    private SharedPreferences.Editor getEditor() {
        return getSharedPreferences().edit();
    }

    private SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(BaseApplication.getInstance());
    }

    /* 网络获得的数据 */
    public String getValueByKey(String key) {
        return getSharedPreferences().getString(key, "");
    }

    public long getLongValueByKey(String key) {
        return getSharedPreferences().getLong(key, 0);
    }

    public void removeKey(String key) {
        SharedPreferences.Editor editor = getEditor();
        editor.remove(key);
        editor.commit();
    }

    public synchronized void setKeyValue(String key, String value) {
        SharedPreferences.Editor editor = getEditor();
        editor.putString(key, value);
        editor.commit();
    }

    public synchronized void setLongValue(String key, long value) {
        SharedPreferences.Editor editor = getEditor();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * 设置是否第一次进入主界面
     * 判断首次登录
     */
    public boolean setFirstEnter(boolean isFirstEnter) {
        SharedPreferences.Editor editor = getEditor();
        editor.putBoolean("KEY_FIRST_ENTER", isFirstEnter);
        return editor.commit();
    }

    /**
     * 是否是第一次进入主界面 true 第一次进入主界面
     *
     * @return
     */
    public boolean isFirstEntry() {
        return getSharedPreferences().getBoolean("KEY_FIRST_ENTER", true);
    }

    /**
     * 判断桌面快捷方式是否生成
     * 设置是否第一次加载
     */
    public boolean setFirstLauncher(boolean isFirstEnter) {
        SharedPreferences.Editor editor = getEditor();
        editor.putBoolean("KEY_FIRST_LAUNCHER", isFirstEnter);
        return editor.commit();
    }

    /**
     * 判断桌面快捷方式是否生成
     * 是否是第一次加载
     *
     * @return
     */
    public boolean isFirstLauncher() {
        return getSharedPreferences().getBoolean("KEY_FIRST_LAUNCHER", true);
    }

}
